package me.pplay.playtalk.activity;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.net.SkyidOperation;
import me.pplay.playtalk.protocol.DataCallBack;
import me.pplay.playtalk.protocol.TpAuth;
import me.pplay.playtalk.protocol.bean.RspData;
import me.pplay.playtalk.protocol.bean.TPBuddyinfo;
import me.pplay.playtalk.util.LogManager;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SignUpActivity extends BaseActivity implements OnClickListener {
	private TpAuth tpAuth;
	private Button signup_btn;
	private EditText signup_username, signup_phonenumber, signup_pwd, signup_confirmpwd;

	private ProgressDialog progressDialog;
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case TpAuth.AUTH_GETPWDPROTECTION_OK:
				LogManager.LogShow("SignUpActivity", "handleMessage AUTH_GETPWDPROTECTION_OK");
				// test code start
				if (false) {// textinfo ����
					Intent intent = new Intent(SignUpActivity.this, SignUpSetQuestionActivity.class);
					startActivity(intent);
					return;
				}
				// test code end
				
				break;
			case TpAuth.AUTH_GETPWDPROTECTION_ERR: {
				LogManager.LogShow("SignUpActivity", "handleMessage AUTH_GETPWDPROTECTION_ERR");
				progressDialog.dismiss();
				// loadDialog.cancel();
				RspData rspdata = (RspData) msg.obj;
				showToast(rspdata.getString() == null ? "Get Protection Unkown err" : rspdata.getString());
			}
				break;
			case TpAuth.AUTH_GETPWDPROTECTION_TIMEOUT:
				LogManager.LogShow("SignUpActivity", "handleMessage AUTH_GETPWDPROTECTION_TIMEOUT");
				progressDialog.dismiss();
				showToast("Time out");
				break;
			case TpAuth.AUTH_SIGUP_EXPLICIT_OK: {
				LogManager.LogShow("SignUpActivity", "handleMessage AUTH_SIGUP_EXPLICIT_OK");
				progressDialog.dismiss();
				Intent intent = new Intent(SignUpActivity.this, SignUpSetQuestionActivity.class);
				Bundle bundle = new Bundle();
				String username = signup_username.getText().toString().trim();
				String password = signup_pwd.getText().toString().trim();
				bundle.putString("username", username);
				bundle.putString("password", password);
				intent.putExtras(bundle);
				startActivity(intent);
				SkyidOperation.saveSkyid(SignUpActivity.this);
				SkyidOperation.savePassword(SignUpActivity.this, password);
				SkyidOperation.saveUserName(SignUpActivity.this, username);
				SignUpActivity.this.finish();
			}
				break;
			case TpAuth.AUTH_SIGUP_EXPLICIT_ERR: {
				LogManager.LogShow("SignUpActivity", "handleMessage AUTH_SIGUP_EXPLICIT_ERR");
				progressDialog.dismiss();
				RspData rspdata = (RspData) msg.obj;
				showToast(rspdata.getString() == null ? "Signup Unkown err" : rspdata.getString());
			}
				break;
			case TpAuth.AUTH_SIGUP_EXLICIT_TIMEOUT:
				LogManager.LogShow("SignUpActivity", "handleMessage AUTH_SIGUP_EXLICIT_TIMEOUT");
				progressDialog.dismiss();
				showToast("Time out");
				break;
			default:
				break;
			}
		}

	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.signup_btn: {
			String username = signup_username.getText().toString().trim();
			String password = signup_pwd.getText().toString().trim();
			String confirmpassword = signup_confirmpwd.getText().toString().trim();
			String phonenumber = signup_phonenumber.getText().toString().trim();
			
			if (username.equals("") || password.equals("") || confirmpassword.equals("") || phonenumber.equals("")) {
				showToast(getString(R.string.string_isnull));
				return;
			}
			if (!password.equals(confirmpassword)) {
				showToast(getString(R.string.password_iswrong));
				return;
			}
			progressDialog = ProgressDialog.show(this, getString(R.string.loading), getString(R.string.waittng), true, false); 
			progressDialog.show();
			if (tpAuth.tp_auth_get_pwdprotection_list().size() > 0)
				signUp();
			else
				getQuestionList();
		}
			break;
		default:
			break;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_signup);
		ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
		tpAuth = appState.getTpAuth();
		findViews();
	}

	/**
	 * Get View by viewId
	 */
	private void findViews() {
		signup_btn = (Button) findViewById(R.id.signup_btn);
		signup_username = (EditText) findViewById(R.id.signup_username);
		signup_phonenumber = (EditText) findViewById(R.id.signup_phonenumber);
		signup_pwd = (EditText) findViewById(R.id.signup_pwd);
		signup_confirmpwd = (EditText) findViewById(R.id.signup_confirmpwd);
		signup_btn.setOnClickListener(this);
	}

//	private void showToast(String str) {
//		Toast.makeText(SignUpActivity.this, str, Toast.LENGTH_SHORT).show();
//	}


	private void getQuestionList() {
		tpAuth.tp_auth_get_pwdprotection(new DataCallBack() {

			@Override
			public void Callback(int result, RspData resultdata, Object obj) {
				Message msg = new Message();
				msg.what = result;
				msg.obj = resultdata;
				LogManager.LogShow("SignUpActivity", "tp_auth_signup_explicit SignUpActivity Callback");
				if (TpAuth.AUTH_GETPWDPROTECTION_OK == result) {
					LogManager.LogShow("SignUpActivity", "AUTH_GETPWDPROTECTION_OK");
					handler.sendMessage(msg);
					signUp();
				} else if (TpAuth.AUTH_GETPWDPROTECTION_ERR == result) {
					LogManager.LogShow("SignUpActivity", "AUTH_GETPWDPROTECTION_ERR");
					handler.sendMessage(msg);
				} else if (TpAuth.AUTH_GETPWDPROTECTION_TIMEOUT == result) {
					LogManager.LogShow("SignUpActivity", "AUTH_GETPWDPROTECTION_TIMEOUT");
					handler.sendMessage(msg);
				}
			}
		});
	}

	private void signUp() {
		String username = signup_username.getText().toString().trim();
		String password = signup_pwd.getText().toString().trim();
		String confirmpassword = signup_confirmpwd.getText().toString().trim();
		String phonenumber = signup_phonenumber.getText().toString().trim();

		tpAuth.tp_auth_signup_explicit(username, confirmpassword, new TPBuddyinfo(), 0, new DataCallBack() {
 
			@Override
			public void Callback(int result, RspData resultdata, Object obj) {
				// TODO Auto-generated method stub
				Message msg = new Message();
				msg.what = result;
				msg.obj = resultdata; 
				LogManager.LogShow("SignUpActivity", "tp_auth_signup_explicit SignUpActivity Callback");
				if (TpAuth.AUTH_SIGUP_EXPLICIT_OK == result) {
					LogManager.LogShow("SignUpActivity", "AUTH_SIGUP_EXPLICIT_OK");
					handler.sendMessage(msg);
				} else if (TpAuth.AUTH_SIGUP_EXPLICIT_ERR == result) {
					LogManager.LogShow("SignUpActivity", "AUTH_SIGIN_EXPLICIT_ERR");
					handler.sendMessage(msg);
				} else if (TpAuth.AUTH_SIGUP_EXLICIT_TIMEOUT == result) {
					LogManager.LogShow("SignUpActivity", "AUTH_SIGUP_EXLICIT_TIMEOUT");
					handler.sendMessage(msg);
				} 
			}
		});

	}
}
